package org.bugjail.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.bugjail.data.Person;
import org.bugjail.data.Project;
import org.bugjail.data.TestDesign;
import org.bugjail.dataaccess.DAOFactory;
import org.bugjail.dataaccess.objects.ProjectDAO;
import org.bugjail.dataaccess.objects.TestDesignDAO;
import org.bugjail.servlet.exception.MissingParameterException;
import org.bugjail.servlet.exception.PermissionException;

/**
 * @author Edwin Carlo
 * 
 * Servlet implementation class TestDesignServlet
 * 
 */
public class TestDesignServlet extends BasicUIServlet {
	
	static final long serialVersionUID = 1L;
	
	private Project project;
	
	@Override
	protected void write(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		
		//Person user = ServletUtilities.getLogedUser(request);
		TestDesignDAO testDesignDAO = DAOFactory.getDAOFactory().getTestDesignDAO();
		List<TestDesign> testDesigns = testDesignDAO.retrieveTestDesignsOfProject(project);

		PrintWriter pw = response.getWriter();

		pw.println(showPath(project));

		pw.println("<table width='100%'><tr>");
		//pw.println("<td align=left><a href='ProjectServlet'><img src='images/up.png' /> Go to projects</a></td>");
		pw.println("<td align=right><a href='NewTestDesignFormServlet?projectid="+project.getId()+"'><img src='images/new.png' /> Create new test design</a></td>");
		pw.println("</tr></table>");		
		//pw.println("<br>");
		
		pw.println("<table width='100%'>");
		pw.println("<tr><td width='100%'><table class='hometable'>");			
		
		if(testDesigns.size() != 0){
			pw.println("<tr><th>Name</th><th>Description</th><th width=40 colspan=2/></tr>");
			for (int i = 0; i < testDesigns.size(); i++) {
				pw.println("<tr>" +
						     "<td><a href='SuiteServlet?testdesignid="+testDesigns.get(i).getId()+"'>"+ testDesigns.get(i).getName() +"</a></td>" +
						     "<td>"+ testDesigns.get(i).getDescription() +"</td>" +
						     "<td width=20><a href='EditTestDesignFormServlet?testdesignid="+testDesigns.get(i).getId()+"'><img src='images/edit.png' /></a></td>" +
						     "<td width=20><img src='images/delete.png' /></td>" +
						   "</tr>");
			}
		} else {
			pw.println("<tr><th>There is no test design for the selected project.</th></tr>");
		}
		
		pw.println("</table></td>");
		pw.print("</tr></table>");
		pw.print("<br>");
	}

	@Override
	protected String pageTitle() {
		return "Test designs of "+project.getName();
	}

	@Override
	protected void beforeAction(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		if (request.getParameter("project") != null) {
			int projectId = Integer.parseInt(request.getParameter("project"));
			ProjectDAO jdbcProject = DAOFactory.getDAOFactory().getProjectDAO();
			Project tempProject = jdbcProject.retrieveById(projectId);
			
			Person user = getLoggedUser();
			if (!user.getProjects().keySet().contains(tempProject)) {
				throw new PermissionException();
			}
			
			this.project = tempProject;
		} else {
			throw new MissingParameterException();
		}
	}

}